home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP08.ZIP / CHAP08 / PATRON / PAGES.CPP < prev    next >
C/C++ Source or Header  |  1993-06-07  |  27KB  |  1,204 lines

  1. /*
  2.  * PAGES.CPP
  3.  * Modifications for Chapter 8
  4.  *
  5.  * Implementation of the CPages class.  See PAGEWIN.CPP for additional
  6.  * member functions.
  7.  *
  8.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  9.  *
  10.  * Kraig Brockschmidt, Software Design Engineer
  11.  * Microsoft Systems Developer Relations
  12.  *
  13.  * Internet  :  kraigb@microsoft.com
  14.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  15.  */
  16.  
  17.  
  18.  
  19. #include "patron.h"
  20.  
  21.  
  22. HWND        g_hDlgPrint=NULL;
  23. BOOL        g_fCancelPrint=FALSE;
  24.  
  25.  
  26. /*
  27.  * CPages:CPages
  28.  * CPages::~CPages
  29.  *
  30.  * Constructor Parameters:
  31.  *  hInst           HINSTANCE of the application we're in.
  32.  *  cf              UINT application clipboard format.
  33.  */
  34.  
  35. CPages::CPages(HINSTANCE hInst, UINT cf)
  36.     : CWindow(hInst)
  37.     {
  38.     m_pPageCur=NULL;
  39.     m_iPageCur=0xFFFF;  //Pages are 0 indexed, so this is one before that.
  40.     m_cPages=0;
  41.     m_hWndPageList=NULL;
  42.  
  43.     /*
  44.      * Initialize to 2.54cm*2.54cm which is a page with no space for anything,
  45.      * just margins.  2.54cm=.5 inches on each margin.
  46.      */
  47.     m_cx=LOMETRIC_PER_INCH;
  48.     m_cy=LOMETRIC_PER_INCH;
  49.  
  50.     m_xPos=0L;
  51.     m_yPos=0L;
  52.  
  53.     m_dwIDNext=0;
  54.     m_pIStorage=NULL;
  55.  
  56.     m_fDirty=FALSE;
  57.     m_cf=cf;
  58.  
  59.     //CHAPTER8MOD
  60.     m_fDragSource=FALSE;
  61.     m_fMoveInPage=FALSE;
  62.  
  63.     m_fDragRectShown=FALSE;
  64.  
  65.     m_uScrollInset=GetProfileInt("windows", "DragScrollInset", DD_DEFSCROLLINSET);
  66.     m_uScrollDelay=GetProfileInt("windows", "DragScrollDelay", DD_DEFSCROLLDELAY);
  67.  
  68.     m_uHScrollCode=0;
  69.     m_uVScrollCode=0;
  70.     //End CHAPTER8MOD
  71.     return;
  72.     }
  73.  
  74.  
  75. CPages::~CPages(void)
  76.     {
  77.     //Insure memory is cleaned up in list, and do final IStorage::Release
  78.     FIStorageSet(NULL, FALSE, FALSE);
  79.  
  80.     if (NULL!=m_hFont && !m_fSystemFont)
  81.         DeleteObject(m_hFont);
  82.  
  83.     if (NULL!=m_hWndPageList)
  84.         DestroyWindow(m_hWndPageList);
  85.  
  86.     //m_hWnd destroyed with the document.
  87.     return;
  88.     }
  89.  
  90.  
  91.  
  92. /*
  93.  * CPages::FIsDirty
  94.  *
  95.  * Purpose:
  96.  *  Tells the caller (document) if anything's happened to dirty us.
  97.  *
  98.  * Parameters:
  99.  *  None
  100.  *
  101.  * Return Value:
  102.  *  None
  103.  */
  104.  
  105. BOOL CPages::FIsDirty(void)
  106.     {
  107.     return m_fDirty;
  108.     }
  109.  
  110.  
  111.  
  112. /*
  113.  * CPages::FInit
  114.  *
  115.  * Purpose:
  116.  *  Instantiates a pages window within a given parent.  The
  117.  *  parent may be a main application window, could be an MDI child
  118.  *  window. We really do not care.
  119.  *
  120.  * Parameters:
  121.  *  hWndParent      HWND of the parent of this window
  122.  *  pRect           LPRECT that this window should occupy
  123.  *  dwStyle         DWORD containing the window's style flags.  Should
  124.  *                  contain WS_CHILD | WS_VISIBLE in typical circumstances.
  125.  *  uID             UINT ID to associate with this window
  126.  *  pv              LPVOID unused for now.
  127.  *
  128.  * Return Value:
  129.  *  BOOL            TRUE if the function succeeded, FALSE otherwise.
  130.  */
  131.  
  132. BOOL CPages::FInit(HWND hWndParent, LPRECT pRect, DWORD dwStyle
  133.     , UINT uID, LPVOID pv)
  134.     {
  135.     int     cy;
  136.  
  137.     m_hWnd=CreateWindowEx(WS_EX_NOPARENTNOTIFY, SZCLASSPAGES
  138.         , SZCLASSPAGES, dwStyle, pRect->left, pRect->top
  139.         , pRect->right-pRect->left, pRect->bottom-pRect->top
  140.         , hWndParent, (HMENU)uID, m_hInst, (LPVOID)this);
  141.  
  142.     if (NULL==m_hWnd)
  143.         return FALSE;
  144.  
  145.     /*
  146.      * Create the hidden listbox we'll use to track pages.  We give it
  147.      * the owner-draw style so we can just store pointers in it.  We have
  148.      * to set the parent to NULL such that the window hangs around after
  149.      * the pages window is destroyed so that we can clean up the
  150.      * memory stored in it from the CPages destructor.
  151.      */
  152.     m_hWndPageList=CreateWindow("listbox", "Page List", WS_POPUP | LBS_OWNERDRAWFIXED
  153.         , 0, 0, 100, 100, HWND_DESKTOP, NULL, m_hInst, NULL);
  154.  
  155.     if (NULL==m_hWndPageList)
  156.         return FALSE;
  157.  
  158.     //Create a 14 point Arial font, or use the system variable font.
  159.     cy=MulDiv(-14, LOMETRIC_PER_INCH, 72);
  160.     m_hFont=CreateFont(cy, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE
  161.         , ANSI_CHARSET, OUT_TT_PRECIS, CLIP_TT_ALWAYS, PROOF_QUALITY
  162.         , VARIABLE_PITCH | FF_SWISS, "Arial");
  163.  
  164.     if (NULL==m_hFont)
  165.         {
  166.         m_hFont=(HFONT)GetStockObject(ANSI_VAR_FONT);
  167.         m_fSystemFont=TRUE;
  168.         }
  169.  
  170.     return TRUE;
  171.     }
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178. /*
  179.  * CPages::FIStorageSet
  180.  *
  181.  * Purpose:
  182.  *  Provides the document's IStorage to the pages for its own uses.
  183.  *  If this is a new storage, then we initalize it with streams we
  184.  *  want to always exists.  If this is an open, then we create
  185.  *  our page list from the PageList string we wrote before.
  186.  *
  187.  * Parameters:
  188.  *  pIStorage       LPSTORAGE to the new or opened storage.  If this is
  189.  *                  NULL then we just clean up and exit.
  190.  *  fChange         BOOL indicating is this was a Save As operation
  191.  *                  meaning that we have the structure already, we
  192.  *                  just need to change our value of m_pIStorage.
  193.  *  fInitNew        BOOL indicating if this is a new storage or one
  194.  *                  opened from a previous save.
  195.  *
  196.  * Return Value:
  197.  *  None
  198.  */
  199.  
  200. BOOL CPages::FIStorageSet(LPSTORAGE pIStorage, BOOL fChange, BOOL fInitNew)
  201.     {
  202.     DWORD           dwMode=STGM_DIRECT | STGM_READWRITE | STGM_SHARE_EXCLUSIVE;
  203.     HRESULT         hr;
  204.     LPPAGE          pPage;
  205.     BOOL            fRet=FALSE;
  206.     ULONG           cbRead;
  207.     PAGELIST        pgList;
  208.     LPSTREAM        pIStream;
  209.     LPMALLOC        pIMalloc;
  210.     LPDWORD         pdwID;
  211.     UINT            i;
  212.  
  213.     //If we're just changing saved roots, just open the current page again
  214.     if (fChange)
  215.         {
  216.         if (NULL==pIStorage)
  217.             return FALSE;
  218.  
  219.         m_pIStorage->Release();
  220.         m_pIStorage=pIStorage;
  221.         m_pIStorage->AddRef();
  222.  
  223.         FPageGet(m_iPageCur, &m_pPageCur, TRUE);
  224.         return TRUE;
  225.         }
  226.  
  227.     if (NULL!=m_hWndPageList)
  228.         {
  229.         //On new or open, clean out whatever it is we have.
  230.         for (i=0; i < m_cPages; i++)
  231.             {
  232.             if (FPageGet(i, &pPage, FALSE))
  233.                 delete pPage;
  234.             }
  235.  
  236.         SendMessage(m_hWndPageList, LB_RESETCONTENT, 0, 0L);
  237.         }
  238.  
  239.     if (NULL!=m_pIStorage)
  240.         m_pIStorage->Release();
  241.  
  242.     m_pIStorage=NULL;
  243.  
  244.     //If we're just cleaning up, then we're done.
  245.     if (NULL==pIStorage)
  246.         return TRUE;
  247.  
  248.     m_pIStorage=pIStorage;
  249.     m_pIStorage->AddRef();
  250.  
  251.     //If this is a new storage, create the streams we require
  252.     if (fInitNew)
  253.         {
  254.         //Page list header.
  255.         hr=m_pIStorage->CreateStream(SZSTREAMPAGELIST, dwMode | STGM_CREATE
  256.             , 0, 0, &pIStream);
  257.  
  258.         if (FAILED(hr))
  259.             return FALSE;
  260.  
  261.         pIStream->Release();
  262.  
  263.         //Device Configuration
  264.         hr=m_pIStorage->CreateStream(SZSTREAMDEVICECONFIG, dwMode | STGM_CREATE
  265.             , 0, 0, &pIStream);
  266.  
  267.         if (FAILED(hr))
  268.             return FALSE;
  269.  
  270.         pIStream->Release();
  271.         return TRUE;
  272.         }
  273.  
  274.  
  275.     /*
  276.      * We're opening an existing file:
  277.      *  1)  Configure for the device we're on
  278.      *  2)  Read the Page List and create page entries for each.
  279.      */
  280.  
  281.     ConfigureForDevice();
  282.  
  283.     //Read the page list.
  284.     hr=m_pIStorage->OpenStream(SZSTREAMPAGELIST, NULL, dwMode, 0, &pIStream);
  285.  
  286.     if (FAILED(hr))
  287.         return FALSE;
  288.  
  289.     if (SUCCEEDED(CoGetMalloc(MEMCTX_SHARED, &pIMalloc)))
  290.         {
  291.         pIStream->Read((LPVOID)&pgList, sizeof(PAGELIST), &cbRead);
  292.         m_cPages  =(UINT)pgList.cPages;
  293.         m_iPageCur=(UINT)pgList.iPageCur;
  294.         m_dwIDNext=pgList.dwIDNext;
  295.  
  296.         fRet=TRUE;
  297.         cbRead=pgList.cPages*sizeof(DWORD);
  298.  
  299.         if (0!=cbRead)
  300.             {
  301.             pdwID=(LPDWORD)pIMalloc->Alloc(cbRead);
  302.  
  303.             if (NULL!=pdwID)
  304.                 {
  305.                 pIStream->Read((LPVOID)pdwID, cbRead, &cbRead);
  306.  
  307.                 for (i=0; i < m_cPages; i++)
  308.                     fRet &=FPageAdd(-1, *(pdwID+i), FALSE); //-1==end of list
  309.  
  310.                 pIMalloc->Free((LPVOID)pdwID);
  311.